package com.vma.jifu.controller;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import com.vma.tools.GetPathByDate;
import com.vma.tools.MDA;
import com.vma.tools.ReturnsUtil;
import com.vma.tools.qiniu.QiniuUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by chenzui on 2017/3/28.
 */
public class FileController extends Controller {
    /**
     * upload 图片上传方法
     * @throws IOException
     */
    public void upload() throws IOException {
        //根据时间生成文件夹目录
        UploadFile file = getFile();
        String imgName = file.getFileName();

        File source = new File(PathKit.getWebRootPath() + "/temp/" + imgName); // 获取临时文件对象
//        String savePath = MDA.fileWebPath +"/"+ GetPathByDate.getPath();
//        FileInputStream fis = new FileInputStream(source);
////
//        File targetDir = new File(savePath);
//        if (!targetDir.exists()) {
//            targetDir.mkdirs();
//        }
//
//        File target = new File(targetDir, imgName);
//        if (!target.exists()) {
//            target.createNewFile();
//        }
//
//        FileOutputStream fos = new FileOutputStream(target);
//        byte[] bts = new byte[1024 * 20];
//        while (fis.read(bts, 0, 1024 * 20) != -1) {
//            fos.write(bts, 0, 1024 * 20);
//        }
//
//        fos.close();
//        fis.close();
        String bucket = "vma-back";
        String upToken = QiniuUtils.getQiNiuToken(bucket);
        String res = QiniuUtils.upFileToQiNiuZone(upToken,PathKit.getWebRootPath() + "/temp/" + imgName);
        renderJson(ReturnsUtil.returnSuccess( "http://os9l4q76s.bkt.clouddn.com/"+res));
    }


}
